﻿
package com.zhbit.Control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.swing.JOptionPane;

import com.zhbitzwz.cn.DB.dbCon;
import com.zhbitzwz.cn.Model.Book;

/**
 * BookInfo控制层业务
 * @author ZWZ <ZHBIT at 2998732468@qq.com>
 */
public class controlBookInfo {
   	
	static {
		dbCon dbCon = new dbCon();
		System.out.println("Control层启动！");
	}
	//添加书籍信息
	
	//修改书籍信息
	public static boolean changeBookInfo (String bookID,String bookName) {
		String sql = "update Book set BookName = '" + bookName + "' where BookID = '"
				+ bookID + "'";
		if (!dbCon.updateDB(sql, null)) {
			return false;
		}
		return true;
	}
	//删除书籍信息
	public static boolean deleteBookInfo (String bookID) {
		String sql = "delete from Book where BookID = '" + bookID + "'";
		if (!dbCon.delete(sql, null)) {
			return false;
		}
		return true;
	}
	//保存书籍信息
	public static boolean insertBookInfo (Book book) {
		String sql = "insert into Book values('" + book.getBookID()
		+ "','" + book.getBookName() + "','" + book.getAuthor()
		+ "'," + book.getPress() + ",'" + book.getPressDate()
		+ "','" + book.getStatus() + "')";
		if (!dbCon.insertDB(sql, null)) {
			JOptionPane.showMessageDialog(null, "插入书籍信息失败，检查是否添加了重复编号");
			return false;
		}
		return true;
	}
	//查询书籍信息（一个字段索引）
	public static List<Book> getAllBookInfoSimple(Book book) {
		List<Book> list = new ArrayList<Book>();
		String sql = "select * from Book where BookID = '" + book.getBookID() 
		+ "' and BookName = '" + book.getBookName() 
		+ "'and Author = '" + book.getAuthor()
		+ "'";
		ResultSet resultSet = dbCon.executeQueryRS(sql, null);
		try {
			while (resultSet.next()) {
				Book bookInfo = new Book();
				bookInfo.setBookID(resultSet.getString("BookID").trim());
				bookInfo.setBookName(resultSet.getString("BookName").trim());
				bookInfo.setAuthor(resultSet.getString("Author").trim());
				bookInfo.setPress(resultSet.getString("Press").trim());
				bookInfo.setPressDate(resultSet.getString("PressDate").trim());
				bookInfo.setStatus(resultSet.getString("Status").trim());
				list.add(bookInfo);
				//TODO BookID信息打印
				System.out.println(resultSet.getString("BookID"));
			}
		} catch (SQLException e) {
			System.out.println("获取所有书籍信息出错！");
			e.printStackTrace();
		}
		return null;
		
	}
	//获取所有书籍信息（所有字段索引）
	public static List<Book> getAllBookInfo() {
		List<Book> list = new ArrayList<Book>();
		ResultSet resultSet = dbCon.executeQueryRS("select * from Book", null);
		try {
			while (resultSet.next()) {
				Book bookInfo = new Book();
				bookInfo.setBookID(resultSet.getString("BookID").trim());
				bookInfo.setBookName(resultSet.getString("BookName").trim());
				bookInfo.setAuthor(resultSet.getString("Author").trim());
				bookInfo.setPress(resultSet.getString("Press").trim());
				bookInfo.setPressDate(resultSet.getString("PressDate").trim());
				bookInfo.setStatus(resultSet.getString("Status").trim());
				list.add(bookInfo);
				//TODO BookID信息打印
				System.out.println(resultSet.getString("BookID"));
			}
		} catch (SQLException e) {
			System.out.println("获取所有书籍信息出错！");
			e.printStackTrace();
		}
		return list;
	} 
}
